<?php

namespace Modules\Inventory\Repository;

use App\Services\UnitService;
use Illuminate\Support\Facades\DB;
use Modules\Inventory\Models\Inventory;
use Modules\Inventory\Models\InventoryPlan;
use Modules\Inventory\Models\InventoryTrans;
use Modules\Warehouse\Models\Stash;
use Modules\Warehouse\Models\Storagelocation;

/**
 * 盘点方案相关
 * Class InventoryPlanRepository
 * @package Modules\Inventory\Repository
 */
class InventoryPlanRepository
{

    /**
     * 盘点方案列表
     * @param $params
     * @return mixed
     */
    static public function getList($params){

        $builder = self::getWhere($params);

        $limit = isset($request['limit']) ? $request['limit'] : 100;

        $list = $builder->orderBy('id','desc')->paginate($limit)->toArray();

        return $list;
    }

    /**
     * 列表条件
     * @param $params
     * @return InventoryOrder
     */
    static public function getWhere($params){

        $builder = new InventoryPlan();
        $canSearchFields = $builder->getFields();
        foreach ($params as $key => $val){
            if(!in_array($key,$canSearchFields) || $val == ''){
                continue;
            }

            if($key == 'status'){
                $builder = $builder->whereIn($key,explode(',',trim($val,',')));
            }else{
                $builder = $builder->where($key,$val);
            }

        }

        return $builder;
    }

    /**
     * 创建盘点方案
     * @param $postData
     * @return mixed
     */
    static public function storeBy($postData){
        $postData['plan_no']    = UnitService::createDocno('PDFA');
        $postData['wms_range']  = json_encode($postData['wms_range']);
        $postData['inventory_eadline']  = !empty($postData['inventory_eadline']) ? strtotime($postData['inventory_eadline']) : 0;
        return (new InventoryPlan())->storeBy($postData);
    }

    /**
     * 盘点方案审核
     * @param $postData
     */
    static public function confirm($planId){

        //盘点方案
        $plan = InventoryPlan::find($planId);

        if($plan->status == 1){
            throw new \Exception('盘点方案已审核');
        }

        $location = json_decode($plan->wms_range,true);

        try{

            DB::beginTransaction();

            foreach ($location as $wmsItem){

                $inv = InventoryRepository::getInventoryByLocationCode($plan->org_code,$wmsItem);

                //盘点单创建
                InventoryOrderRepository::createrOrder($plan,$inv,$wmsItem);

            }

            $plan->update(['status' => 1]);

            DB::commit();

            return true;

        }catch (\Exception $e){
            DB::rollBack();
            throw $e;
        }
    }

    /**
     * 根据id查询一条盘点方案信息
     * @param $planId
     * @return mixed
     */
    static public function getFirstPlanById($planId){
        return InventoryPlan::find($planId);
    }

    /**
     * 按盘点方案id更新数据
     * @param $planId
     * @param $data
     * @return mixed
     */
    static public function updateById($planId,$data){
        return InventoryPlan::where('id',$planId)->update($data);
    }


}
